package com.caigou.modules.center.ui.detail

import android.os.Bundle
import android.view.MotionEvent
import android.view.inputmethod.InputMethodManager
import com.alibaba.android.arouter.facade.annotation.Autowired
import com.alibaba.android.arouter.facade.annotation.Route
import com.alibaba.android.arouter.launcher.ARouter
import com.caigou.comm.base.constants.AppConstants
import com.caigou.comm.base.constants.RouterPath
import com.caigou.comm.base.container.BaseActivity
import com.caigou.modules.center.databinding.ActivityDetailBinding
import com.caigou.modules.center.ui.detail.dynamic.*
import dagger.hilt.android.AndroidEntryPoint

/**
 * @author #Suyghur.
 * Created on 2021/11/03
 */
@Route(path = RouterPath.Center.PAGE_DETAIL)
@AndroidEntryPoint
class DetailActivity : BaseActivity() {

    private lateinit var binding: ActivityDetailBinding
    private var myFollowFragment: MyFollowFragment? = null
    private var myFansFragment: MyFansFragment? = null
    private var myDynamicFragment: MyDynamicFragment? = null

    private var hkTicketFragment: HkTicketFragment? = null
    private var macaoTicketFragment: MacaoTicketFragment? = null
    private var singaporeTicketFragment: SingaporeTicketFragment? = null
    private var twTicketFragment: TwTicketFragment? = null
    private var soccerTicketFragment: SoccerTicketFragment? = null
    private var infoFragment: InfoFragment? = null
    private var marketFragment: MarketFragment? = null
    private var hkGalleryFragment: HkGalleryFragment? = null
    private var macaoGalleryFragment: MacaoGalleryFragment? = null
    private var dataFragment: DataFragment? = null
    private var lawFragment: LawFragment? = null
    private var guessFragment: GuessFragment? = null
    private var runDogFragment: RunDogFragment? = null
    private var unlikeFragment: UnlikeFragment? = null
    private var boardFragment: BoardFragment? = null

    private var myScoreFragment: MyScoreFragment? = null
    private var flowerFragment: FlowerFragment? = null
    private var renameCardFragment: RenameCardFragment? = null
    private var deleteCardFragment: DeleteCardFragment? = null
    private var freeCardFragment: FreeCardFragment? = null
    private var aboutScoreFragment: AboutScoreFragment? = null
    private var scoreDetailFragment: ScoreDetailFragment? = null


    @Autowired(name = AppConstants.TARGET_FRAGMENT_TAG)
    lateinit var targetFragmentTag: String

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        binding = ActivityDetailBinding.inflate(layoutInflater)
        setContentView(binding.root)
        containerId = binding.detailFlContainer.id
        initView()
    }

    private fun initView() {
        ARouter.getInstance().inject(this)
        switchFragment(targetFragmentTag)
    }

    override fun switchFragment(tag: String) {
        when (tag) {
            RouterPath.Center.TAG_MY_FOLLOW -> {
                if (myFollowFragment == null) {
                    myFollowFragment = MyFollowFragment()
                }
                switchFragment(myFollowFragment!!, tag)
            }
            RouterPath.Center.TAG_MY_FANS -> {
                if (myFansFragment == null) {
                    myFansFragment = MyFansFragment()
                }
                switchFragment(myFansFragment!!, tag)
            }
            RouterPath.Center.TAG_MY_DYNAMIC -> {
                if (myDynamicFragment == null) {
                    myDynamicFragment = MyDynamicFragment()
                }
                switchFragment(myDynamicFragment!!, tag)
            }
            RouterPath.Center.TAG_HK_TICKET_DYNAMIC -> {
                if (hkTicketFragment == null) {
                    hkTicketFragment = HkTicketFragment()
                }
                switchFragment(hkTicketFragment!!, tag)
            }
            RouterPath.Center.TAG_MACAO_TICKET_DYNAMIC -> {
                if (macaoTicketFragment == null) {
                    macaoTicketFragment = MacaoTicketFragment()
                }
                switchFragment(macaoTicketFragment!!, tag)
            }
            RouterPath.Center.TAG_SINGAPORE_TICKET_DYNAMIC -> {
                if (singaporeTicketFragment == null) {
                    singaporeTicketFragment = SingaporeTicketFragment()
                }
                switchFragment(singaporeTicketFragment!!, tag)
            }
            RouterPath.Center.TAG_TW_TICKET_DYNAMIC -> {
                if (twTicketFragment == null) {
                    twTicketFragment = TwTicketFragment()
                }
                switchFragment(twTicketFragment!!, tag)
            }
            RouterPath.Center.TAG_SOCCER_TICKET_DYNAMIC -> {
                if (soccerTicketFragment == null) {
                    soccerTicketFragment = SoccerTicketFragment()
                }
                switchFragment(soccerTicketFragment!!, tag)
            }
            RouterPath.Center.TAG_INFO_DYNAMIC -> {
                if (infoFragment == null) {
                    infoFragment = InfoFragment()
                }
                switchFragment(infoFragment!!, tag)
            }
            RouterPath.Center.TAG_MARKET_DYNAMIC -> {
                if (marketFragment == null) {
                    marketFragment = MarketFragment()
                }
                switchFragment(marketFragment!!, tag)
            }
            RouterPath.Center.TAG_HK_GALLERY_DYNAMIC -> {
                if (hkGalleryFragment == null) {
                    hkGalleryFragment = HkGalleryFragment()
                }
                switchFragment(hkGalleryFragment!!, tag)
            }
            RouterPath.Center.TAG_MACAO_GALLERY_DYNAMIC -> {
                if (macaoGalleryFragment == null) {
                    macaoGalleryFragment = MacaoGalleryFragment()
                }
                switchFragment(macaoGalleryFragment!!, tag)
            }
            RouterPath.Center.TAG_DATA_DYNAMIC -> {
                if (dataFragment == null) {
                    dataFragment = DataFragment()
                }
                switchFragment(dataFragment!!, tag)
            }
            RouterPath.Center.TAG_LAW_DYNAMIC -> {
                if (lawFragment == null) {
                    lawFragment = LawFragment()
                }
                switchFragment(lawFragment!!, tag)
            }
            RouterPath.Center.TAG_GUESS_DYNAMIC -> {
                if (guessFragment == null) {
                    guessFragment = GuessFragment()
                }
                switchFragment(guessFragment!!, tag)
            }
            RouterPath.Center.TAG_RUN_DOG_DYNAMIC -> {
                if (runDogFragment == null) {
                    runDogFragment = RunDogFragment()
                }
                switchFragment(runDogFragment!!, tag)
            }
            RouterPath.Center.TAG_UNLIKE_DYNAMIC -> {
                if (unlikeFragment == null) {
                    unlikeFragment = UnlikeFragment()
                }
                switchFragment(unlikeFragment!!, tag)
            }
            RouterPath.Center.TAG_BOARD_DYNAMIC -> {
                if (boardFragment == null) {
                    boardFragment = BoardFragment()
                }
                switchFragment(boardFragment!!, tag)
            }
            RouterPath.Center.TAG_SCORE -> {
                if (myScoreFragment == null) {
                    myScoreFragment = MyScoreFragment()
                }
                switchFragment(myScoreFragment!!, tag)
            }
            RouterPath.Center.TAG_FLOWER -> {
                if (flowerFragment == null) {
                    flowerFragment = FlowerFragment()
                }
                switchFragment(flowerFragment!!, tag)
            }
            RouterPath.Center.TAG_RENAME_CARD -> {
                if (renameCardFragment == null) {
                    renameCardFragment = RenameCardFragment()
                }
                switchFragment(renameCardFragment!!, tag)
            }
            RouterPath.Center.TAG_DELETE_CARD -> {
                if (deleteCardFragment == null) {
                    deleteCardFragment = DeleteCardFragment()
                }
                switchFragment(deleteCardFragment!!, tag)
            }
            RouterPath.Center.TAG_FREE_CARD -> {
                if (freeCardFragment == null) {
                    freeCardFragment = FreeCardFragment()
                }
                switchFragment(freeCardFragment!!, tag)
            }
            RouterPath.Center.TAG_ABOUT_SCORE -> {
                if (aboutScoreFragment == null) {
                    aboutScoreFragment = AboutScoreFragment()
                }
                switchFragment(aboutScoreFragment!!, tag)
            }
            RouterPath.Center.TAG_SCORE_DETAIL -> {
                if (scoreDetailFragment == null) {
                    scoreDetailFragment = ScoreDetailFragment()
                }
                switchFragment(scoreDetailFragment!!, tag)
            }
        }
    }


    override fun dispatchTouchEvent(ev: MotionEvent?): Boolean {
        ev?.let {
            if (it.action == MotionEvent.ACTION_DOWN) {
                val v = currentFocus
                if (isShouldHideInput(v, it)) {
                    val imm =
                        this@DetailActivity.getSystemService(INPUT_METHOD_SERVICE) as InputMethodManager
                    imm.hideSoftInputFromWindow(v!!.windowToken, 0)
                }
                return super.dispatchTouchEvent(it)
            }
        }
        // 必不可少，否则所有的组件都不会有TouchEvent了
        return if (window.superDispatchTouchEvent(ev)) {
            true
        } else onTouchEvent(ev)
    }

    override fun onBackPressed() {
        if (fragmentStack.isNotEmpty() && fragmentStack.size > 1) {
            fragmentStack.removeLast()
            switchFragment(fragmentStack.last)
        } else {
            super.onBackPressed()
        }
    }
}